package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.CallCenterAdminDTO;
import com.xbongbong.paas.pojo.dto.CallCenterAgentDTO;
import com.xbongbong.paas.pojo.dto.CallCenterSupervisorDTO;
import com.xbongbong.paas.pojo.dto.CallSocketReckonDTO;
import com.xbongbong.paas.pojo.dto.CallUpdateAccountDTO;
import com.xbongbong.paas.pojo.vo.CallCenterAdminVO;
import com.xbongbong.paas.pojo.vo.CallCenterAgentVO;
import com.xbongbong.paas.pojo.vo.CallCenterSupervisorVO;
import com.xbongbong.paas.pojo.vo.CallSocketReckonVO;
import com.xbongbong.paas.pojo.vo.CallUpdateAccountVO;
import com.xbongbong.paas.service.CallCenterAccountService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 呼叫中心数据设置统计
 * @date 2019/2/28 19:42
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/callCenter/account")
public class CallCenterAccountController {

    @Resource
    private CallCenterAccountService callCenterAccountService;

    /**
     * 员工设置坐席状态
     *
     * @return 员工设置坐席状态
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/agent", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setAgent(@RequestBody @Valid CallCenterAgentDTO callCenterAgentDTO, BindingResult br) {
        XbbResponse<CallCenterAgentVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterAccountService.agent(callCenterAgentDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 员工设置团队主管
     *
     * @return 员工设置团队主管
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/supervisor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String accountSupervisor(@RequestBody @Valid CallCenterSupervisorDTO callCenterSupervisorDTO, BindingResult br) {
        XbbResponse<CallCenterSupervisorVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterAccountService.accountSupervisor(callCenterSupervisorDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 员工设置超管状态
     *
     * @return 员工设置超管状态
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/admin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String accountAdmin(@RequestBody @Valid CallCenterAdminDTO callCenterAdminDTO, BindingResult br) {
        XbbResponse<CallCenterAdminVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterAccountService.accountAdmin(callCenterAdminDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取并更新客户端及版本号
     *
     * @return 获取并更新客户端及版本号
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/updateAccount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateAccount(@RequestBody @Valid CallUpdateAccountDTO callUpdateAccountDTO, BindingResult br) {
        XbbResponse<CallUpdateAccountVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterAccountService.updateAccount(callUpdateAccountDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 呼叫统计
     *
     * @param callSocketReckonDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/6/26 15:29
     * @since v1.0
     */
    @RequestMapping(value = "/socketReckon", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String socketReckon(@RequestBody @Valid CallSocketReckonDTO callSocketReckonDTO, BindingResult br) {
        XbbResponse<CallSocketReckonVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CallSocketReckonVO callSocketReckonVO = callCenterAccountService.reckon(callSocketReckonDTO);
                response = new XbbResponse<>(callSocketReckonVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
